﻿using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using Maticsoft.DBUtility;//Please add references
namespace hcrainMvc.DAL
{
	/// <summary>
	/// 数据访问类:hcr_w_run
	/// </summary>
	public partial class hcr_w_run
	{
		public hcr_w_run()
		{}
		#region  BasicMethod

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(long id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from hcr_w_run");
			strSql.Append(" where id=@id");
			MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int64)
			};
			parameters[0].Value = id;

			return DbHelperMySQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(hcrainMvc.Model.hcr_w_run model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into hcr_w_run(");
			strSql.Append("fruntype,frunywid,fdeid,fnid,fnname,fntype,faddtime,fstates,fywstate,fsetuptime,fiscallback,fywcreuid,fywsubuid)");
			strSql.Append(" values (");
			strSql.Append("@fruntype,@frunywid,@fdeid,@fnid,@fnname,@fntype,@faddtime,@fstates,@fywstate,@fsetuptime,@fiscallback,@fywcreuid,@fywsubuid)");
			MySqlParameter[] parameters = {
					new MySqlParameter("@fruntype", MySqlDbType.VarChar,100),
					new MySqlParameter("@frunywid", MySqlDbType.VarChar,100),
					new MySqlParameter("@fdeid", MySqlDbType.VarChar,100),
					new MySqlParameter("@fnid", MySqlDbType.VarChar,100),
					new MySqlParameter("@fnname", MySqlDbType.VarChar,50),
					new MySqlParameter("@fntype", MySqlDbType.VarChar,50),
					new MySqlParameter("@faddtime", MySqlDbType.DateTime),
					new MySqlParameter("@fstates", MySqlDbType.VarChar,100),
					new MySqlParameter("@fywstate", MySqlDbType.VarChar,255),
					new MySqlParameter("@fsetuptime", MySqlDbType.Timestamp),
					new MySqlParameter("@fiscallback", MySqlDbType.VarChar,100),
					new MySqlParameter("@fywcreuid", MySqlDbType.VarChar,30),
					new MySqlParameter("@fywsubuid", MySqlDbType.VarChar,50)};
			parameters[0].Value = model.fruntype;
			parameters[1].Value = model.frunywid;
			parameters[2].Value = model.fdeid;
			parameters[3].Value = model.fnid;
			parameters[4].Value = model.fnname;
			parameters[5].Value = model.fntype;
			parameters[6].Value = model.faddtime;
			parameters[7].Value = model.fstates;
			parameters[8].Value = model.fywstate;
			parameters[9].Value = model.fsetuptime;
			parameters[10].Value = model.fiscallback;
			parameters[11].Value = model.fywcreuid;
			parameters[12].Value = model.fywsubuid;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(hcrainMvc.Model.hcr_w_run model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update hcr_w_run set ");
			strSql.Append("fruntype=@fruntype,");
			strSql.Append("frunywid=@frunywid,");
			strSql.Append("fdeid=@fdeid,");
			strSql.Append("fnid=@fnid,");
			strSql.Append("fnname=@fnname,");
			strSql.Append("fntype=@fntype,");
			strSql.Append("faddtime=@faddtime,");
			strSql.Append("fstates=@fstates,");
			strSql.Append("fywstate=@fywstate,");
			strSql.Append("fiscallback=@fiscallback,");
			strSql.Append("fywcreuid=@fywcreuid,");
			strSql.Append("fywsubuid=@fywsubuid");
			strSql.Append(" where id=@id");
			MySqlParameter[] parameters = {
					new MySqlParameter("@fruntype", MySqlDbType.VarChar,100),
					new MySqlParameter("@frunywid", MySqlDbType.VarChar,100),
					new MySqlParameter("@fdeid", MySqlDbType.VarChar,100),
					new MySqlParameter("@fnid", MySqlDbType.VarChar,100),
					new MySqlParameter("@fnname", MySqlDbType.VarChar,50),
					new MySqlParameter("@fntype", MySqlDbType.VarChar,50),
					new MySqlParameter("@faddtime", MySqlDbType.DateTime),
					new MySqlParameter("@fstates", MySqlDbType.VarChar,100),
					new MySqlParameter("@fywstate", MySqlDbType.VarChar,255),
					new MySqlParameter("@fiscallback", MySqlDbType.VarChar,100),
					new MySqlParameter("@fywcreuid", MySqlDbType.VarChar,30),
					new MySqlParameter("@fywsubuid", MySqlDbType.VarChar,50),
					new MySqlParameter("@id", MySqlDbType.Int64,20)};
			parameters[0].Value = model.fruntype;
			parameters[1].Value = model.frunywid;
			parameters[2].Value = model.fdeid;
			parameters[3].Value = model.fnid;
			parameters[4].Value = model.fnname;
			parameters[5].Value = model.fntype;
			parameters[6].Value = model.faddtime;
			parameters[7].Value = model.fstates;
			parameters[8].Value = model.fywstate;
			parameters[9].Value = model.fiscallback;
			parameters[10].Value = model.fywcreuid;
			parameters[11].Value = model.fywsubuid;
			parameters[12].Value = model.id;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(long id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from hcr_w_run ");
			strSql.Append(" where id=@id");
			MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int64)
			};
			parameters[0].Value = id;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from hcr_w_run ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public hcrainMvc.Model.hcr_w_run GetModel(long id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,fruntype,frunywid,fdeid,fnid,fnname,fntype,faddtime,fstates,fywstate,fsetuptime,fiscallback,fywcreuid,fywsubuid from hcr_w_run ");
			strSql.Append(" where id=@id");
			MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int64)
			};
			parameters[0].Value = id;

			hcrainMvc.Model.hcr_w_run model=new hcrainMvc.Model.hcr_w_run();
			DataSet ds=DbHelperMySQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public hcrainMvc.Model.hcr_w_run DataRowToModel(DataRow row)
		{
			hcrainMvc.Model.hcr_w_run model=new hcrainMvc.Model.hcr_w_run();
			if (row != null)
			{
				if(row["id"]!=null && row["id"].ToString()!="")
				{
					model.id=long.Parse(row["id"].ToString());
				}
				if(row["fruntype"]!=null)
				{
					model.fruntype=row["fruntype"].ToString();
				}
				if(row["frunywid"]!=null)
				{
					model.frunywid=row["frunywid"].ToString();
				}
				if(row["fdeid"]!=null)
				{
					model.fdeid=row["fdeid"].ToString();
				}
				if(row["fnid"]!=null)
				{
					model.fnid=row["fnid"].ToString();
				}
				if(row["fnname"]!=null)
				{
					model.fnname=row["fnname"].ToString();
				}
				if(row["fntype"]!=null)
				{
					model.fntype=row["fntype"].ToString();
				}
				if(row["faddtime"]!=null && row["faddtime"].ToString()!="")
				{
					model.faddtime=DateTime.Parse(row["faddtime"].ToString());
				}
				if(row["fstates"]!=null)
				{
					model.fstates=row["fstates"].ToString();
				}
				if(row["fywstate"]!=null)
				{
					model.fywstate=row["fywstate"].ToString();
				}
				if(row["fsetuptime"]!=null && row["fsetuptime"].ToString()!="")
				{
					model.fsetuptime=DateTime.Parse(row["fsetuptime"].ToString());
				}
				if(row["fiscallback"]!=null)
				{
					model.fiscallback=row["fiscallback"].ToString();
				}
				if(row["fywcreuid"]!=null)
				{
					model.fywcreuid=row["fywcreuid"].ToString();
				}
				if(row["fywsubuid"]!=null)
				{
					model.fywsubuid=row["fywsubuid"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,fruntype,frunywid,fdeid,fnid,fnname,fntype,faddtime,fstates,fywstate,fsetuptime,fiscallback,fywcreuid,fywsubuid ");
			strSql.Append(" FROM hcr_w_run ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperMySQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM hcr_w_run ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from hcr_w_run T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperMySQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			MySqlParameter[] parameters = {
					new MySqlParameter("@tblName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@fldName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@PageSize", MySqlDbType.Int32),
					new MySqlParameter("@PageIndex", MySqlDbType.Int32),
					new MySqlParameter("@IsReCount", MySqlDbType.Bit),
					new MySqlParameter("@OrderType", MySqlDbType.Bit),
					new MySqlParameter("@strWhere", MySqlDbType.VarChar,1000),
					};
			parameters[0].Value = "hcr_w_run";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperMySQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

